package com.lowca.news.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.compass.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lowca.news.constant.Constants.RoleMeta;
import com.lowca.news.dao.support.Pager;
import com.lowca.news.entity.User;
import com.lowca.news.service.RoleService;
import com.lowca.news.service.UserService;
import com.lowca.news.util.Captchas;
import com.lowca.news.web.formbean.AdminUserConditionForm;
import com.lowca.news.web.jsonbean.UserWarpperBean;

@Controller
public class AdminUserController {

	@Resource
	private UserService userService;

	@Resource
	private RoleService roleService;

	@RequestMapping(value = "/admin/user/list.do", method = RequestMethod.GET)
	public String userList(HttpServletRequest request) throws ServletException,
			IOException {
		return "/admin/user/list";
	}

	@RequestMapping(value = "/admin/user/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userListSubmit(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "jCondition", required = false) String jCondition,
			@RequestParam(value = "start", required = true) int start,
			@RequestParam(value = "limit", required = true) int limit)
			throws ServletException, IOException {
		AdminUserConditionForm condition = null;
		if (StringUtils.hasText(jCondition)) {
			ObjectMapper mapper = new ObjectMapper();
			mapper.registerSubtypes(AdminUserConditionForm.class);
			condition = mapper.readValue(jCondition,
					AdminUserConditionForm.class);
		}
		int pageNo = start / limit + 1;
		Pager<User> userPage = userService.searchUser(condition, pageNo, limit);
		List<UserWarpperBean> list = warpUser(userPage);
		Map<String, Object> jMap = new HashMap<String, Object>();
		jMap.put("count", userPage.getTotalCount());
		jMap.put("dataset", list);
		return jMap;
	}

	private List<UserWarpperBean> warpUser(Pager<User> page) {
		List<UserWarpperBean> waperList = new ArrayList<UserWarpperBean>();
		for (User user : page.getList()) {
			waperList.add(new UserWarpperBean(user));
		}
		return waperList;
	}

	@RequestMapping(value = "/admin/user/set-role.do", method = RequestMethod.GET)
	public String setRole(HttpServletRequest request) throws ServletException,
			IOException {
		return "/admin/user/set-role";
	}

	@RequestMapping(value = "/admin/user/set-role.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> setRoleSubmit(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "captcha", required = true) String captcha,
			@RequestParam(value = "operate", required = true) int operate)
			throws ServletException, IOException {
		Map<String, Object> msgMap = new HashMap<String, Object>();
		msgMap.put("success", true);
		if (!Captchas.check(request, captcha)) {
			msgMap.put("msg", "验证码错误 ");
			return msgMap;
		}
		User user = userService.getUserByUsername(username);
		if (user == null) {
			msgMap.put("msg", "用户不存在 ");
			return msgMap;
		}
		String[] roleNames = { RoleMeta.DEVELOPER.getName(),
				RoleMeta.ADMIN.getName() };
		boolean isAdmin = roleService.hasAnyRoles(user, roleNames);
		if (operate == 1) {
			if (isAdmin) {
				msgMap.put("msg", "当前用户已经是管理员了，不能重复设置 ");
				return msgMap;
			}
			roleService.addRoleToUser(user, RoleMeta.ADMIN.getName());
		} else if (operate == 0) {
			if (!isAdmin) {
				msgMap.put("msg", "当前用户不是管理员，不能进行取消管理员操作 ");
				return msgMap;
			}
			roleService.removeRoleFromUser(user, RoleMeta.ADMIN.getName());
		}
		msgMap.put("msg", "ok");
		return msgMap;
	}
}
